<?php 
declare (strict_types = 1);
namespace app\admin\controller;
use app\common\controller\Admin;
use think\Request;
class Script extends Admin
{
    protected $table_id = false;
    protected $with = ['business','script_screen','script_setting_nd','script_setting_xs','script_class.script_setting','script_role'];
    public function initialize()
    {
        $this->menu_input = [
            'all' => [
            ],
            'add' => [
            ],
            'upload' => [
            ],
        ];
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function table_mage(&$data = [])
    {
    
        $data = [
            ['field' => 'name', 'title' => '剧本名称'],
            ['field' => 'type_name', 'title' => '来源'],
            ['field' => 'class_name', 'title' => '剧本类型'],
            ['field' => 'nd', 'title' => '剧本难度'],
            ['field' => 'xs', 'title' => '剧本形式'],
            ['title' => '操作', 'templet' => '#list_setting']
        ];
        return parent::table_mage($data); // TODO: Change the autogenerated stub
    }
    public function index()
    {

        $ScriptSetting_model = app('app\admin\model\ScriptSetting');
        $script_setting_nd = $ScriptSetting_model->where('p_id',2)->select();
        $this->assign('script_setting_nd',$script_setting_nd);
        $script_setting_xs = $ScriptSetting_model->where('p_id',3)->select();
        $this->assign('script_setting_xs',$script_setting_xs);
        $script_class = $ScriptSetting_model->where('p_id',1)->select();
        $this->assign('script_class',$script_class);
        return parent::index(); // TODO: Change the autogenerated stub
    }
    public function read()
    {
        $id = input('id');
        $data = $this->model->where('id',$id)->with($this->with)->find()->toArray();
        if(!empty($data['images'])){
            $data['images'] = explode(',',$data['images']);
        }else{
            $data['images'] = [];
        }
        $ScriptSetting_model = app('app\admin\model\ScriptSetting');
        $script_setting_nd = $ScriptSetting_model->where('p_id',2)->select();
        $this->assign('script_setting_nd',$script_setting_nd);
        $script_setting_xs = $ScriptSetting_model->where('p_id',3)->select();
        $this->assign('script_setting_xs',$script_setting_xs);
        $script_class = $ScriptSetting_model->where('p_id',1)->select();
        $this->assign('script_class',$script_class);
        $this->assign('data', $data);
        $Screen_model = app('app\admin\model\Screen');
        $Screen_data = $Screen_model->where('p_id',0)->with('p_screen')->select()->toArray();
        $this->assign('sx',$Screen_data);
        return view();
    }
    public function index_handle(&$data)
    {
        $res = $data['data'];
        foreach ($res as $k=>$v)
        {
            $res[$k]['nd'] = $v['script_setting_nd']['name'] ?? '';
            $res[$k]['xs'] = $v['script_setting_xs']['name'] ?? '';
            $type_name = '';
            if($v['type'] == 1){
                $type_name = '平台添加';
            }elseif($v['type'] == 2){
                $type_name = '门店添加';
            }
            $class_name = '';
            foreach ($v['script_class'] as $kk=>$vv)
            {
                if(empty($class_name)){
                    $class_name = $vv['script_setting']['name'];
                }else{
                    $class_name .= '&nbsp&nbsp'.$vv['script_setting']['name'];
                }
            }
            $res[$k]['class_name'] = $class_name;
            $res[$k]['type_name'] = $type_name;
        }
        $data['data'] = $res;
        return parent::index_handle($data); // TODO: Change the autogenerated stub
    }
    //列表-数据接口
    public function datalist()
    {
        if (is_array($this->with)) {
            $with = $this->with;
        } else {
            $with = [];
        }
        $data = param();
        unset($data['page']);
        unset($data['limit']);
        unset($data['parent']);
        foreach ($data as $k => $v) {
            if ($k == 'create_time') {
                $arr = b_arr($v, '-');
                $first = b_str([$arr[0], $arr[1], $arr[2]], '-');
                $last = b_str([$arr[3], $arr[4], $arr[5]], '-');
                $this->where[] = ['create_time', '>=', trim($first)];
                $this->where[] = ['create_time', '<=', trim($last)];
            }elseif($k == 'id'){
                $this->where[] = [$k, 'like', '%' . $v . '%'];
            } elseif($k == 'script_class_id'){
                $ScriptClass_model = app('app\admin\model\ScriptClass');
                $arr = $ScriptClass_model->where('script_setting_id',$v)->select();
                if(!empty($arr) && !empty($arr->toArray())){
                    $arr = $arr->toArray();
                    $ids = '';
                    foreach ($arr as $kk=>$vv){
                        if(empty($ids)){
                            $ids = (string)$vv['script_id'];
                        }else{
                            $ids .= (','.$vv['script_id']);
                        }
                    }
                    $this->where[] = ['id','in',$ids];
                }
            } else {
                $this->where[] = [$k, 'like', '%' . $v . '%'];
            }
        }
        if ($this->parent_model && input('parent')) {
            if ($this->request->controller() == 'Menu') {
                $this->where[] = [lcfirst($this->parent_model) . 'id', '=', input('parent')];
            } else {
                $this->where[] = [lcfirst($this->parent_model) . '_id', '=', input('parent')];
            }
        }
        $order = $this->order ?: 'id desc';
        $this->index_where($this->where);
        if(!empty($this->where)){
            $this->where = array_values($this->where);
        }
        $list = $this->model->where($this->where)->order($order)->with($with)->paginate($this->limit)->toArray();
        $this->index_handle($list);
        return layui($list['data'], $list['total']);
    }
    public function add()
    {
        $ScriptSetting_model = app('app\admin\model\ScriptSetting');
        $script_setting_nd = $ScriptSetting_model->where('p_id',2)->select();
        $this->assign('script_setting_nd',$script_setting_nd);
        $script_setting_xs = $ScriptSetting_model->where('p_id',3)->select();
        $this->assign('script_setting_xs',$script_setting_xs);
        $script_class = $ScriptSetting_model->where('p_id',1)->select();
        $this->assign('script_class',$script_class);
        //筛选项
        $Screen_model = app('app\admin\model\Screen');
        $Screen_data = $Screen_model->where('p_id',0)->with('p_screen')->select()->toArray();
        $this->assign('sx',$Screen_data);
        return parent::add(); // TODO: Change the autogenerated stub
    }
    //编辑-页面
    public function upload()
    {
        $id = input('id');
        $data = $this->model->where('id', $id)->with($this->with)->find()->toArray();
        if(!empty($data['images'])){
            $data['images'] = explode(',',$data['images']);
        }else{
            $data['images'] = [];
        }
        $ScriptSetting_model = app('app\admin\model\ScriptSetting');
        $script_setting_nd = $ScriptSetting_model->where('p_id',2)->select();
        $this->assign('script_setting_nd',$script_setting_nd);
        $script_setting_xs = $ScriptSetting_model->where('p_id',3)->select();
        $this->assign('script_setting_xs',$script_setting_xs);
        $script_class = $ScriptSetting_model->where('p_id',1)->select();
        $this->assign('script_class',$script_class);
        $this->assign('data', $data);
        $Screen_model = app('app\admin\model\Screen');
        $Screen_data = $Screen_model->where('p_id',0)->with('p_screen')->select()->toArray();
        $this->assign('sx',$Screen_data);

        return view();
    }

    //添加-数据接口
    public function add_ajax()
    {
        if (request()->isAjax()) {
            $post = array_filter(input('post.'));
            if ($post) {
                if(mb_strlen($post['name']) > 20) return msgerror('名称不能大于20个字符');
                if(intval($post['sex_m']) > 1000 || intval($post['sex_m']) < 1) return msgerror('男人数不能大于1000或小于1');
                if(intval($post['sex_f']) > 1000 || intval($post['sex_f']) < 1) return msgerror('女人数不能大于1000或小于1');
                $post['price'] = intval($post['price']);
                if($post['price'] > (100*10000)) return msgerror('价格不能大于100W');
                if(isset($post['images']) && !empty($post['images'])){
                    //轮播图
                    $post['images'] = implode(',',$post['images']);
                }else{
                    $post['images'] = '';
                }
                $script_class = false;
                if(isset($post['script_class']) && !empty($post['script_class']))
                {
                    //类型
                    $script_class = $post['script_class'];
                    if(count($script_class) > 4) return msgerror('类型不能大于4个');
                }
                $role = false;
                if(isset($post['role']) && !empty($post['role'])){
                    //角色
                    $role = $post['role'];
                }
                //筛选内容
                $sx = false;
                if(isset($post['sx']) && !empty($post['sx'])){
                    $sx = $post['sx'];
                }
                unset($post['role']);
                unset($post['script_class']);
                unset($post['sx']);
                $this->model->save($post);
                $id = $this->model->id;
                if($script_class){
                    $ScriptClass_model = app('app\admin\model\ScriptClass');
                    //添加类型到中间表中
                    $arr = [];
                    foreach ($script_class as $k=>$v)
                    {
                        $arr[] = ['script_id'=>$id,'script_setting_id'=>$v];
                    }
                    $ScriptClass_model->saveAll($arr);
                }
                if($sx){
                    $ScriptScreen_model = app('app\admin\model\ScriptScreen');
                    //添加筛选到中间表中
                    $arr = [];
                    foreach ($sx as $k=>$v)
                    {
                        $arr[] = ['script_id'=>$id,'screen_id'=>$v];
                    }
                    $ScriptScreen_model->saveAll($arr);
                }
                if($role){
                    $ScriptRole_model = app('app\admin\model\ScriptRole');
                    //绑定添加的角色到剧本上
                    foreach($role as $k=>$v)
                    {
                        $ScriptRole_model::update(['id'=>$v,'script_id'=>$id]);
                    }
                }
            }
            return datasuccess($post);
        }
    }
    //编辑-数据接口
    public function upload_ajax()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            if ($post) {
                if(mb_strlen($post['name']) > 20) return msgerror('名称不能大于20个字符');
                if(intval($post['sex_m']) > 1000 || intval($post['sex_m']) < 1) return msgerror('男人数不能大于1000或小于1');
                if(intval($post['sex_f']) > 1000 || intval($post['sex_f']) < 1) return msgerror('女人数不能大于1000或小于1');
                $post['price'] = intval($post['price']);
                if($post['price'] > (100*10000)) return msgerror('价格不能大于100W');
                if(isset($post['images']) && !empty($post['images'])){
                    //轮播图
                    $post['images'] = implode(',',$post['images']);
                }else{
                    $post['images'] = '';
                }
                $script_class = false;
                if(isset($post['script_class']) && !empty($post['script_class']))
                {
                    //类型
                    $script_class = $post['script_class'];
                    if(count($script_class) > 4) return msgerror('类型不能大于4个');
                }
                $role = false;
                if(isset($post['role']) && !empty($post['role'])){
                    //角色
                    $role = $post['role'];
                }
                //筛选内容
                $sx = false;
                if(isset($post['sx']) && !empty($post['sx'])){
                    $sx = $post['sx'];
                }
                unset($post['role']);
                unset($post['script_class']);
                unset($post['sx']);
                $ScriptClass_model = app('app\admin\model\ScriptClass');
                $ScriptClass_model->where('script_id',$post['id'])->delete();
                if($script_class){
                    //添加类型到中间表中
                    $arr = [];
                    foreach ($script_class as $k=>$v)
                    {
                        $arr[] = ['script_id'=>$post['id'],'script_setting_id'=>$v];
                    }
                    $ScriptClass_model->saveAll($arr);
                }
                $ScriptScreen_model = app('app\admin\model\ScriptScreen');
                $ScriptScreen_model->where('script_id',$post['id'])->delete();
                if($sx){
                    //添加筛选到中间表中
                    $arr = [];
                    foreach ($sx as $k=>$v)
                    {
                        $arr[] = ['script_id'=>$post['id'],'screen_id'=>$v];
                    }
                    $ScriptScreen_model->saveAll($arr);
                }
                if($role){
                    $ScriptRole_model = app('app\admin\model\ScriptRole');
                    //绑定添加的角色到剧本上
                    foreach($role as $k=>$v)
                    {
                        $ScriptRole_model::update(['id'=>$v,'script_id'=>$post['id']]);
                    }
                }
                $this->model::update($post);
            }
            return datasuccess($post);
        }
    }
}